#include "lineups.h"
#include "ui_lineups.h"
#include "playerradio.h"
#include "QDebug"

Lineups::Lineups(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Lineups)
{
    ui->setupUi(this);
}


Lineups::~Lineups()
{
    delete ui;
}


void Lineups::prepareWindow() {
    LoadHomePlayers();
    LoadAwayPlayers();
    PopulatePitchHome();
    PopulatePitchAway();
    this->show();

}

void Lineups::setHome(Team* home) {

    home_team = home;
}


void Lineups::setAway(Team* away) {

    away_team = away;
}

void Lineups::LoadHomePlayers() {

    home_team->getRoster();
    home_team->debugRoster();

    foreach(Player* p, home_team->returnRoster()){
        PlayerRadio* radiobutton = new PlayerRadio(this);
        radiobutton->setPlayer(p);
        radiobutton->setText(p->getLastName());
        radiobutton->setJersy(p->getJerseyNumber());
        radiobutton->setLast(p->getLastName());
        home_players.append(radiobutton);
        ui->home_layout->addWidget(radiobutton);
        qDebug() << "HOME";
    }
}

void Lineups::LoadAwayPlayers() {

    away_team->getRoster();

    foreach(Player* p, away_team->returnRoster()){
        PlayerRadio* radiobutton = new PlayerRadio(this);
        radiobutton->setPlayer(p);
        radiobutton->setText(p->getLastName());
        radiobutton->setLast(p->getLastName());
        radiobutton->setJersy(p->getJerseyNumber());
        away_players.append(radiobutton);
        ui->away_layout->addWidget(radiobutton);
    }

}


void Lineups::PopulatePitchHome() {


       foreach(PlayerRadio *each_player, home_players) {
           ui->comboBox->addItem(each_player->getLast(), QVariant(each_player->getJersy()));
             }


}

void Lineups::PopulatePitchAway() {
    foreach(PlayerRadio *each_player, away_players) {
        ui->comboBox_2->addItem(each_player->getLast(), QVariant(each_player->getJersy()));
          }
}

void Lineups::StartGame() {
    // Get Home and away Pitcher Jerseys
    int home_pitcher_jersy = ui->comboBox->itemData(ui->comboBox->currentIndex()).toInt();
    int away_pitcher_jersy = ui->comboBox_2->itemData(ui->comboBox_2->currentIndex()).toInt();

    qDebug() << "Home Jersy From Combo " << home_pitcher_jersy << " : " << away_pitcher_jersy;

    // Add Players
    int i=0;
    foreach(PlayerRadio* each_radio, home_players) {
        if(each_radio->getJersy()==home_pitcher_jersy)
            home_pitcher = i;
        if(each_radio->isChecked()) {
            home_players_vector.append(each_radio->getPlayer());
            i++;
        }



    }
    i=0;
    foreach(PlayerRadio* each_radio, away_players) {
        if(each_radio->getJersy()==away_pitcher_jersy)
            away_pitcher = i;
        if(each_radio->isChecked()) {
            away_players_vector.append(each_radio->getPlayer());
            i++;
            }



    }


    // READY TO CALL SCOREKEEPER PASSING home_players_vector away_players_vector

    // DEBUG IF EVERYTHING VECTORS DONE CORRECTLY:

    foreach(Player *p, home_players_vector) {
        qDebug() << "Last: " << p->getLastName();
    }

    qDebug() << "Pitcher Index: " << home_pitcher;

    foreach(Player *p, away_players_vector) {
        qDebug() << "Last: " << p->getLastName();
    }

    qDebug() << "Pitcher Index: " << away_pitcher;

}







